<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
checkLoginHeader();
checkPermission();
categoryAction();
?>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/header.php'; ?>


<script type="text/javascript">
    function validateAddCategoryForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(categoryname,"Categoryname must be filled out!")==false)
                {categoryname.focus();return false;}
              }
            }
        }
    function validateEditCategoryForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(edCategoryname,"Categoryname must be filled out!")==false)
                {edCategoryname.focus();return false;}
              }
            }
        }


    function addCategory(){
        document.getElementById('categoryAction').style.display = '';
        document.getElementById('formAddCategory').style.display = '';
        document.getElementById('formEditCategory').style.display = 'none';
    }
    function editCategory(id){
        document.getElementById('categoryAction').style.display = '';
        document.getElementById('formAddCategory').style.display = 'none';
        document.getElementById('formEditCategory').style.display = '';
        $.ajax({
            url: '/<?php echo $config ['admin_section']; ?>/category',
            type: 'POST',
            dataType: 'json',
            data: "id="+id+"&categoryActionType=editCategory",
            timeout: 1000,
            error: function(){
                alert('Error loading category infomation');
            },
            success: function(data){
                $("#edId").val(data.id);
                $("#edParent_id").val(data.parent_id);
                $("#edCategoryname").val(data.name);
                $("#categoryActionType").val('updateCategory');
            }
        });
    }

    function deleteCategory(id,txtAlert){
        if (confirm("Do you want to delete "+txtAlert+"?")){
            $.ajax({
                url: '/<?php echo $config ['admin_section']; ?>/category',
                type: 'POST',
                data: "id="+id+"&categoryActionType=deleteCategory",
                error: function(){
                    alert('Error loading category infomation');
                },
                success: function(){
                    window.location = "/<?php echo $config ['admin_section']; ?>/category";
                }
            });
        }
    }

    $(document).ready(function(){
        document.getElementById('categoryAction').style.display = 'none';
        setTimeout("closeResultMessages()",5000);
    });
    function closeResultMessages(){
        document.getElementById('rsMessages').style.display = 'none';
    }
    function closeHtml(id){
        document.getElementById(id).style.display = 'none';
    }

</script>

<div id="wrapper">
            <div id="content" style="width: 98%;">

                <div id="categoryAction">
                <div id="box">
                      <div id="rightnow">
                          <h3 class="reallynow">
                            <span>Category Actions</span>
                            <a href="javascript: closeHtml('categoryAction');">Close</a><br />
                          </h3>
                      </div>
                      <div id="formAddCategory">
                          <form method="post" action="/<?php echo $config ['admin_section']; ?>/category" id="form" name="formAddCategory" onsubmit="javascript:return validateAddCategoryForm(this);" style="padding-top: 10px">
                          <fieldset id="fsAddCategory">
                            <legend>Add category</legend>
                            <label for="categoryname">Category name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="categoryname" name="categoryname"/>
                            <input type="hidden" id="categoryActionType" name="categoryActionType" value="addCategory"/>
                            <br>
                           <label for="parent_id">Parent : </label>
                            <select name="parent_id" id="parent_id">
                                <option selected="selected" label="none" value="0">Root</option>
                                <?php
                                    $category = get_cat_tree(0);
                                    foreach($category as $k=>$rs){
                               ?>
                                <option id="article_category_id<?php echo $rs['id'];?>" value="<?php echo $rs['id'];?>"><? echo $rs['name'];?></option>
                               <?php
                                    }
                                ?>

                                

                            </select>

                          </fieldset>

                      <div align="center">
                      <input type="submit" value="Create category" name="addCategorySubmit" id="addCategorySubmit"/>
                      <input type="reset" id="AddCategoryReset"/>
                      </div>
                    </form>
                    </div>

                    <div id="formEditCategory">
                        <form method="post" action="/<?php echo $config ['admin_section']; ?>/category" id="form" onsubmit="javascript:return validateEditCategoryForm(this);" name="formEditCategory">
                          <fieldset id="fsEditCategory">
                            <legend>Edit category</legend>
                            <label for="name">Category name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="edCategoryname" name="edCategoryname"/>
                            <input type="hidden" id="edId" name="edId"/>
                            <input type="hidden" id="categoryActionType" name="categoryActionType" value="updateCategory"/>
                            <br/>
                            <label for="edParent_id">Parent : </label>
                            <select name="edParent_id" id="edParent_id">
                                <option selected="selected" label="none" value="0">Root</option>
                                <?php
                                    //$category = get_cat_tree(0);
                                    foreach($category as $k=>$rs){
                               ?>
                                <option value="<?php echo $rs['id'];?>"><? echo $rs['name'];?></option>
                               <?php
                                    }
                                ?>
                            </select>

                          </fieldset>

                      <div align="center">
                          <input type="submit" value="Update category infomation" name="editCategorySubmit" id="editCategorySubmit"/>
                      </div>
                    </form>
                    </div>
                </div>
                <div style="height:10px"></div>
                </div>

                
                <div id="rightnow">
                    <h3 class="reallynow">
                        <span>Article Category</span>
                        <span style="float: right"><a class="add" href="javascript: addCategory();">Add category</a></span>
                        <br />
                    </h3>
                    <div class="youhave">
                        <div id="rsMessages" style="text-align: center; color: red; font-weight: bolder"><? if(isset($_SESSION['rsMessages'])){echo $_SESSION['rsMessages']; $_SESSION['rsMessages']='';} ?></div>
                        <table width="100%">

                            <tbody>
                                <tr>
                                    <td width="40px">&nbsp;</td>
                                    <td><b>Category name</b></td>

                                    <td width="40px"><b>Action</b></td>
                                </tr>
                                 <?php
                                    //$category = get_cat_tree(0);
                                    $i=0;
                                    foreach($category as $k=>$rs){
                                        $i++;
                                    
                               ?>

                                <tr>
                                    <td class="a-center"><?php echo $i;?></td>
                                    <td>&nbsp;&nbsp;<a href="/<?php echo $config ['admin_section']; ?>/article&articleActionType=searchArticle&categoryId=<?php echo $rs['id'];?>&searchArticleSubmit=Search"><?php echo $rs['name'];?></a></td>
                                    <td><a href="#"><a href="javascript: editCategory('<?php echo $rs['id'];?>');"><img height="16" width="16" title="Edit category" src="<?php echo $config ['admin_images_url'];?>/icons/page_white_edit.png"/></a><a href="javascript: deleteCategory('<?php echo $rs['id'];?>','<?php echo $rs['name'];?>');"><img height="16" width="16" title="Delete category" src="<? echo $config ['admin_images_url'];?>/icons/page_white_delete.png"/></a></td>
                                </tr>

                               <?php
                                    }
                                ?>

                              
                                </tbody>
                        </table>
                    </div>
                  </div>                  
            </div>


      </div>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/footer.php'; ?>